package com.byx.chatclient.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Looper;
import android.os.MessageQueue;
import android.view.View;

import com.byx.chatclient.R;

import java.util.HashMap;
import java.util.Map;

/**
 * GradientStatusBarUtil
 * 用于设置状态栏渐变背景的工具类
 * @author Superb
 * @date 2021/4/9 23:35
 */
public class GradientStatusBarUtil {
    private View mStatusBar;
    private Activity mActivity;
    private int mResId = R.color.colorBlack;

    private GradientStatusBarUtil(){}

    public static GradientStatusBarUtil getInstance(){
        return new GradientStatusBarUtil();
    }

    public GradientStatusBarUtil bind(Activity activity){
        mActivity = activity;
        return this;
    }

    public GradientStatusBarUtil withBackgroundResource(int resId){
        mResId = resId;
        return this;
    }

    private void initStatusBar() {
        if (mStatusBar == null) {
            int identifier = mActivity.getResources().getIdentifier("statusBarBackground", "id", "android");
            mStatusBar = mActivity.getWindow().findViewById(identifier);
        }
        if (mStatusBar != null) {
            mStatusBar.setBackgroundResource(mResId);
        }
    }

    public void init(){
        //延时加载数据.
        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                if (isStatusBar()) {
                    initStatusBar();
                    mActivity.getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                        @Override
                        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                            initStatusBar();
                        }
                    });
                }
                //只走一次
                return false;
            }
        });
    }

    private boolean isStatusBar() {
        return true;
    }

    public void unbind(){
        mActivity = null;
        mStatusBar = null;
    }
}
